package com.pointi.emoTools ;

import java.io.File ;

import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;

import org.w3c.dom.Document ;
import org.w3c.dom.Element ;
import org.w3c.dom.Node ;
import org.w3c.dom.NodeList ;

public class emoLicense
{
	private String[] s_Key = {	" ", "A", "B", "C", "D", "E", "F", "G", "H",
			"I", "J", "K", "L", "M", "N", "O", "P", "Q",
			"R", "S", "T", "U", "V", "W", "X", "Y", "Z" } ;

	// DATABASE
	public String s_database_target = "" ;
	public String s_database_type = "" ;
	public String s_database_version = "" ;
	public String s_database_address = "" ;
	public String s_database_port = "" ;
	public String s_database_name = "" ;
	public String s_database_accountid = "" ;
	public String s_database_accountpw = "" ;

	// SERVER
	public String s_server_ip = "" ;
	public String s_server_port = "" ;
	public String s_server_key = "" ;
	public String s_server_company = "" ;
	public String s_server_service = "" ;
	public String s_server_chtype = "" ;
	public String s_server_channel = "" ;
	public String s_target_company = "" ;
	public String s_target_service = "" ;
	public String s_target_license = "" ;

	public emoLicense() {}

	public void InitializeData()
	{
		// DATABASE
		s_database_target = "" ;
		s_database_type = "" ;
		s_database_version = "" ;
		s_database_address = "" ;
		s_database_port = "" ;
		s_database_name = "" ;
		s_database_accountid = "" ;
		s_database_accountpw = "" ;

		// SERVER
		s_server_ip = "" ;
		s_server_port = "" ;
		s_server_key = "" ;
		s_server_company = "" ;
		s_server_service = "" ;
		s_server_chtype = "" ;
		s_server_channel = "" ;
		s_target_company = "" ;
		s_target_service = "" ;
		s_target_license = "" ;
	}

	public Document ReadConfXML(String strFile, emoLicense conf_data) throws Exception
	{
		NodeList mCATEGORY = null ;
		Node mNode = null ;
		conf_data.InitializeData() ;
		try
		{
			File file = new File(strFile) ;
			DocumentBuilderFactory docBuildFact = DocumentBuilderFactory.newInstance() ;
			DocumentBuilder docBuild = docBuildFact.newDocumentBuilder() ;
			Document doc = docBuild.parse(file) ;
			doc.getDocumentElement().normalize() ;
			if (doc.getDocumentElement().getNodeName().equals("configuration"))
			{
				// CONF | CATEGORY | DATABASE
				mCATEGORY = doc.getElementsByTagName("database") ;
				mNode = mCATEGORY.item(0) ;
				if (mNode.getNodeType() == Node.ELEMENT_NODE)
				{
					Element mElmnt = (Element) mNode ; // TYPE_CAST
					conf_data.s_database_target = fnReadTerminal(mElmnt, "target") ;
					conf_data.s_database_type = fnReadTerminal(mElmnt, "type") ;
					conf_data.s_database_version = fnReadTerminal(mElmnt, "version") ;
					conf_data.s_database_address = fnReadTerminal(mElmnt, "address") ;
					conf_data.s_database_port = fnReadTerminal(mElmnt, "port") ;
					conf_data.s_database_name = fnReadTerminal(mElmnt, "name") ;
					conf_data.s_database_accountid = fnReadTerminal(mElmnt, "account_name") ;
					conf_data.s_database_accountpw = fnReadTerminal(mElmnt, "account_password") ;
				}

				// CONF | CATEGORY | SERVER
				mCATEGORY = doc.getElementsByTagName("server") ;
				mNode = mCATEGORY.item(0) ;
				if (mNode.getNodeType() == Node.ELEMENT_NODE)
				{
					Element mElmnt = (Element) mNode ; // TYPE_CAST
					conf_data.s_server_ip = fnReadTerminal(mElmnt, "address") ;
					conf_data.s_server_port = fnReadTerminal(mElmnt, "port") ;
					conf_data.s_server_key = fnReadTerminal(mElmnt, "key") ;
				}
			}
			return doc ;
		} catch (Exception e) { e.printStackTrace() ; }
		return null ;
	}
	
	protected String fnReadTerminal(Element inElmnt, String inTagName)
	{
		NodeList mList= inElmnt.getElementsByTagName(inTagName) ;
		Element mElmnt = (Element) mList.item(0) ;
		Node mNode = mElmnt.getFirstChild() ;
		return mNode.getNodeValue() ;
	}

	public void WriteLogConfigurationData(emoLicense conf_data)
	{
		System.out.println("----------------- database ------------------") ;
		System.out.println("target		: " + conf_data.s_database_target) ;
		System.out.println("type		: " + conf_data.s_database_type) ;
		System.out.println("version		: " + conf_data.s_database_version) ;
		System.out.println("address		: " + conf_data.s_database_address) ;
		System.out.println("port		: " + conf_data.s_database_port) ;
		System.out.println("name		: " + conf_data.s_database_name) ;
		System.out.println("accountid	: " + conf_data.s_database_accountid) ;
		System.out.println("accountpw	: " + conf_data.s_database_accountpw) ;
		System.out.println("---------------------------------------------") ;
		System.out.println("------------------ server -------------------") ;
		System.out.println("serverip	: " + conf_data.s_server_ip) ;
		System.out.println("serverport	: " + conf_data.s_server_port) ;
		System.out.println("serverkey	: " + conf_data.s_server_key) ;
		System.out.println("---------------------------------------------") ;
	}

	public int GetLicenseChannel(String sLicenseKey)
	{
		int nReturn = 0 ;

		String s_company = sLicenseKey.substring(0, 12) ;
		String s_service = sLicenseKey.substring(12, 18) ;
		String s_wavtype = sLicenseKey.substring(18, 26) ;
		String s_channel = sLicenseKey.substring(26, 32) ;
		String s_tcompany = sLicenseKey.substring(52, 58) ;
		String s_tservice = sLicenseKey.substring(58, 64) ;
		String s_tlicense = sLicenseKey.substring(64, 78) ;
		s_server_company =
				s_Key[Integer.parseInt(s_company.substring(0, 2))] + 
				s_Key[Integer.parseInt(s_company.substring(2, 4))] +
				s_Key[Integer.parseInt(s_company.substring(4, 6))] + 
				s_Key[Integer.parseInt(s_company.substring(6, 8))] + 
				s_Key[Integer.parseInt(s_company.substring(8, 10))] + 
				s_Key[Integer.parseInt(s_company.substring(10, 12))] ;
		s_server_service =
				s_Key[Integer.parseInt(s_service.substring(0, 2))] +
				s_Key[Integer.parseInt(s_service.substring(2, 4))] +
				s_Key[Integer.parseInt(s_service.substring(4, 6))] ;
		s_server_chtype = 
				s_Key[Integer.parseInt(s_wavtype.substring(0, 2))] +
				s_Key[Integer.parseInt(s_wavtype.substring(2, 4))] +
				s_Key[Integer.parseInt(s_wavtype.substring(4, 6))] +
				s_Key[Integer.parseInt(s_wavtype.substring(6, 8))] ;
		s_server_channel = "" +
				Integer.parseInt(s_channel.substring(0, 2)) +
				Integer.parseInt(s_channel.substring(2, 4)) +
				Integer.parseInt(s_channel.substring(4, 6)) + "" ;
		s_target_company = 
				s_Key[Integer.parseInt(s_tcompany.substring(0, 2))] +
				s_Key[Integer.parseInt(s_tcompany.substring(2, 4))] +
				s_Key[Integer.parseInt(s_tcompany.substring(4, 6))] ;
		s_target_service = 
				s_Key[Integer.parseInt(s_tservice.substring(0, 2))] +
				s_Key[Integer.parseInt(s_tservice.substring(2, 4))] +
				s_Key[Integer.parseInt(s_tservice.substring(4, 6))] ;
		s_target_license = 
				s_Key[Integer.parseInt(s_tlicense.substring(0, 2))] +
				s_Key[Integer.parseInt(s_tlicense.substring(2, 4))] +
				s_Key[Integer.parseInt(s_tlicense.substring(4, 6))] +
				s_Key[Integer.parseInt(s_tlicense.substring(6, 8))] +
				s_Key[Integer.parseInt(s_tlicense.substring(8, 10))] +
				s_Key[Integer.parseInt(s_tlicense.substring(10, 12))] +
				s_Key[Integer.parseInt(s_tlicense.substring(12, 14))] ;

		System.out.println("------------------ license ------------------") ;
		System.out.println("company		: " + s_server_company) ;
		System.out.println("service		: " + s_server_service) ;
		System.out.println("ch-type		: " + s_server_chtype) ;
		System.out.println("channel		: " + s_server_channel) ;
		System.out.println("t-company	: " + s_target_company) ;
		System.out.println("t-service	: " + s_target_service) ;
		System.out.println("t-license	: " + s_target_license) ;
		System.out.println("---------------------------------------------") ;

		if (s_server_company.toLowerCase().equals("pointi") && s_server_service.toLowerCase().equals("tam") && s_server_chtype.toLowerCase().equals("mono"))
		{
			if (s_target_company.toLowerCase().equals("keb") && s_target_service.toLowerCase().equals("pds"))
				nReturn = Integer.parseInt(s_server_channel) ;
		}
		return nReturn ;
	}
}